package com.etoak.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MVCConfig implements WebMvcConfigurer {

    /**
     * application.yml 上传目录
     */
    @Value("${upload.dir}")
    private String uploadDirectory;

    /**
     * application.yml 图片地址前缀
     */
    @Value("${upload.path}")
    private String pathPrefix;


    /**
     * mvc:resource location="file: e:/upload/et2104" mapping="/pic/**"
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // location -> file: e:/upload/et2104
        String location = uploadDirectory.endsWith("/") ?
                "file:" + uploadDirectory :
                "file:" + uploadDirectory + "/";

        //mapping -> /pic/**
        String mapping = pathPrefix.endsWith("/") ? pathPrefix + "**" :
                pathPrefix + "/**";

        registry.addResourceHandler(mapping)  // 对应mapping
                .addResourceLocations(location); //对应location
    }
}
